import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../data/profile_data.dart';
import 'change/change_avatar.dart';

class Header extends StatelessWidget {
  const Header({super.key});

  @override
  Widget build(BuildContext context) {
    final userController = Get.find<UserController>();

    return Obx(() {
      if (userController.isReady.value) {
        return _loginBuilder(
          context,
          userController.pictureUrl.value,
          userController.name.value,
          userController.studentId.value,
          userController.credit.value,
        );
      } else {
        return FutureBuilder(
          future: userController.getUserInfo(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.data == true) {
                return _loginBuilder(
                  context,
                  userController.pictureUrl.value,
                  userController.name.value,
                  userController.studentId.value,
                  userController.credit.value,
                );
              } else {
                return Text("加载失败");
              }
            } else {
              return SizedBox(
                height: 95.0,
                child: Center(child: CircularProgressIndicator()),
              );
            }
          },
        );
      }
    });
  }

  Widget _loginBuilder(
    BuildContext context,
    String pictureUrl,
    String username,
    String studentId,
    int credit,
  ) {
    return Container(
      decoration: BoxDecoration(color: Colors.blue),
      height: 100.0,
      child: Row(
        children: <Widget>[
          Container(
            height: 80.0,
            width: 80.0,
            margin: EdgeInsets.only(right: 10.0, left: 10.0),
            child: GestureDetector(
              onTap: () {
                Get.to(const UpdateAvatarPage());
              },
              child: CircleAvatar(
                backgroundImage: CachedNetworkImageProvider(pictureUrl),
              ),
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              SizedBox(height: 20.0),
              Text(
                username,
                style: TextStyle(
                  fontSize: 20.0,
                  color: Colors.white,
                  fontWeight: FontWeight.w500,
                ),
              ),
              SizedBox(height: 5),
              Row(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Text(
                    studentId,
                    style: TextStyle(
                      fontSize: 18.0,
                      color: Colors.white,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  Text(
                    " 信用分: $credit",
                    style: TextStyle(
                      fontSize: 18.5,
                      color: Colors.white,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
}

